var organizationId = $('#organizationId_input').val();
var searchBox = $('#searchItemText');
var searchBtn = $('#searchBtn');
var dataPage = 1;
var selectPageBtn = $('#selectPageBtn');
var lastPageBtn = $('#lastPageBtn');
var nextPageBtn = $('#nextPageBtn');
var itemContainer = $('#itemContainer');
function refreshData(pageNumber,pageSize=9){
	$.ajax({
	  	  type: 'POST', 
	  	  url: '/getItemPage',
	  	  data: {
	  		  "search" : searchBox.val(),
	  		  "organiaztionId" : organizationId,
	  		  "pageNumber" : pageNumber,
	  		  "pageSize" : pageSize
	  	  },
	  	  async:false,
	  	  success: function(data){
	  		  var page = parseInt(data.total/pageSize);
	  		  if(data.total%pageSize!=0)
	  			  page++;
	  		  dataPage = page;
	  		  selectPageBtn.empty();
	  		  for(var i=1;i<=page;i++){
	  			selectPageBtn.append('<option value='+i+'>第'+i+'页</option>  ');
	  		  }
	  		  selectPageBtn.val(pageNumber);
	  		  var rows = data.rows;
	  		  itemContainer.empty();
	  		  for(var j=0;j<rows.length;j++){
	  			itemContainer.append('<div class="pop_small pop_bottom">'+
					 			'<a class="small_top" href="" title="'+rows[j].name+'">'+
									'<img src="'+rows[j].image+'">'+
								'</a>'+
								'<div class="small_bottom">'+
									'<a class="small_title" href="">'+rows[j].name+'</a>'+
									'<span class="line_price small_price">'+rows[j].price+'</span>'+
								'</div>'+
					 		'</div>');
	  		  }
	  	  }
	});
}
$(function(){
	searchBtn.click(function(){
		refreshData(1);
	});
	selectPageBtn.change(function(){
		refreshData(selectPageBtn.val());
	});
	lastPageBtn.click(function(){
		var currentPage = selectPageBtn.val();
		if(currentPage == 1){
			alert('已经是第一页!');
			return;
		}
		else{
			refreshData(--currentPage);
		}
	});
	nextPageBtn.click(function(){
		var currentPage = selectPageBtn.val();
		console.log(currentPage+","+dataPage);
		if(currentPage == dataPage){
			alert('已经是最后一页!');
			return;
		}
		else{
			refreshData(++currentPage);
		}
	});
	refreshData(1);

});
